<template>
  <a-card :bordered="false">
    <a-spin :spinning="confirmLoading" :tip="tips">
      <a-steps class="steps" :current="currentTab">
        <a-step title="选择日期" />
        <a-step title="选择生成用户" />
        <a-step title="选择收费标准" />
        <a-step title="完成" />
      </a-steps>
      <div class="content">
        <step1 v-if="currentTab === 0" @nextStep="nextStep1"/>
        <step2 v-if="currentTab === 1" @nextStep="nextStep2" @prevStep="prevStep" v-bind:params="step1Param"/>
        <step3 v-if="currentTab === 2" @submit="submit" @prevStep="prevStep" v-bind:params="step1Param"/>
        <step4 v-if="currentTab === 3" @prevStep="prevStep" @finish="finish"/>
      </div>
    </a-spin>
  </a-card>
</template>

<script>
import Step1 from './createOrder/Step1'
import Step2 from './createOrder/Step2'
import Step3 from './createOrder/Step3'
import Step4 from './createOrder/Step4'
import { makeOrder } from '@/api/manage'
import { mapGetters } from 'vuex'
import moment from 'moment'

export default {
  name: 'create_fee_order',
  components: {
    Step1,
    Step2,
    Step3,
    Step4
  },
  data () {
    return {
      description: '生成对应日期对应住户的不同费用！',
      currentTab: 0,
      step1Param:'',
      step2Param:'',
      step3Param:'',
      // form
      form: null,
      tips: 'Loading...',
      confirmLoading:false
    }
  },
  methods: {
    ...mapGetters(['token', 'pmsArId', 'nowMg']),
    nextStep1 (time) {
      if (this.currentTab < 3) {
        this.currentTab += 1
      }
      this.step1Param=time
    },
    nextStep2 (data) {
      if (this.currentTab < 3) {
        this.currentTab += 1
      }
      this.step2Param=data;
    },
    submit (data) {
      this.step3Param=data;
      const start_day= moment(this.step1Param.buildTime[0]).format("YYYY-MM-DD")
      const end_day= moment(this.step1Param.buildTime[1]).format("YYYY-MM-DD")
      const houseIds= this.step2Param
      const payType= data
      console.log(houseIds)
      this.confirmLoading= true;
      this.sendDatas (start_day,end_day,houseIds,payType,1)
    },
    sendDatas (start_day,end_day,houseIds,payType,page){
       makeOrder({token: this.token(), ar_id: this.pmsArId(), cm_mg_id: this.nowMg(), start_day: start_day, end_day: end_day, houseIds: houseIds, payType: payType,page:page}).then(res=>{
        if (res.code == 0) {
          this.confirmLoading= false;
          this.currentTab += 1
        } else if(res.code == '1000') {
           page++
           this.tips='共'+res.data+'当前执行'+page+'页'
           this.sendDatas(start_day,start_day,houseIds,payType,page)
        }else{
          this.confirmLoading= false;
          this.$notification['error']({
            message: '错误',
            description: res.msg,
            duration: 4
          })
        }
      })
    },
    // handler
    nextStep () {
      if (this.currentTab < 2) {
        this.currentTab += 1
      }
    },
    prevStep () {
      if (this.currentTab > 0) {
        this.currentTab -= 1
      }
    },
    finish () {
      this.currentTab = 0
    }
  }
}
</script>

<style lang="less" scoped>
    .steps {
        max-width: 750px;
        margin: 16px auto;
    }
</style>
